import { ref } from 'vue'
import { v4 } from 'uuid'
import _ from 'lodash-es'
import Tool from '@/libs/utils/Tool'
import Api from '@/libs/api'
import { useI18n } from 'vue-i18n'
import type { Ref } from 'vue'
import type { GoodsEditForm } from './types'

export const useGoodsSkuGeneration = (
	editForm: Ref<GoodsEditForm>,
	canEditSkuAttrs: Ref<boolean>
) => {
	const { t } = useI18n()
	const skuColumns = ref<any[]>([])
	const attrVSelects = ref<string[]>([])
	const skuSelects = ref<any[]>([])

	// 这个函数需要从 useGoodsForm 中提取
	const getToAddSkuColumns = () => {
		return [
			{
				title: t('商品详细.上级ID'),
				dataIndex: 'copiedById',
				visible: false
			},
			{
				title: t('商品详细.sort'),
				dataIndex: 'sort',
				batchQuantitySet: true
			},
			{
				title: t('商品详细.cover'),
				dataIndex: 'cover',
				batchImageSet: true
			},
			{
				title: t('商品详细.封面ID'),
				dataIndex: 'coverFileId',
				visible: false
			},
			{
				title: t('商品详细.inventory'),
				dataIndex: 'inventory',
				batchQuantitySet: true
			},
			{
				title: t('商品详细.costPrice'),
				dataIndex: 'costPrice',
				batchPriceSet: true
			},
			{
				title: t('商品详细.commissionPrice'),
				dataIndex: 'commissionPrice',
				batchPriceSet: true
			},
			{
				title: t('商品详细.shopPrice'),
				dataIndex: 'shopPrice',
				batchPriceSet: true
			},
			{
				title: t('商品详细.rewardPrice'),
				dataIndex: 'rewardPrice',
				batchPriceSet: true
			},
			{
				title: t('商品详细.salePrice'),
				dataIndex: 'salePrice',
				batchPriceSet: true
			},
			{
				title: t('商品详细.guidePrice'),
				dataIndex: 'guidePrice',
				batchPriceSet: true
			}
		]
	}

	const getAttrVSelects = () => {
		const attrVSelects: string[] = []
		editForm.value.attrs.forEach(attrK => {
			attrK.v.forEach(attrV => {
				if (attrV.isSel) {
					attrVSelects.push(attrV.uuid)
				}
			})
		})
		return attrVSelects
	}

	const generateSkus = () => {
		skuColumns.value = []
		attrVSelects.value = getAttrVSelects()
		const initialValue = [{}]
		const _skus = editForm.value.attrs.reduce(
			(previousValue, currentValue, currentIndex, array) => {
				const attr: any = currentValue
				const values = attr.v
				const _attrK = Tool.deepClone(attr)
				delete _attrK.v
				skuColumns.value.push({
					title: attr.k,
					dataIndex: attr.uuid,
					align: 'center',
					width: 100,
					minWidth: 100,
					customRender: (item: any) => {
						return item.record[attr.uuid].attrV.value
					}
				})
				return previousValue
					.map(sku => {
						return values.map(v => {
							const _attrV = Tool.deepClone(v)
							if (!sku[_attrV.value]) {
								if (!sku.vIds) sku.vIds = []
								if (!sku.vUuids) sku.vUuids = []
								const newSku = {
									...sku,
									id: 0,
									key: v4(),
									isSel: false,
									vIds: [...sku.vIds, _attrV.id],
									vUuids: [...sku.vUuids, _attrV.uuid],
									[attr.uuid]: {
										attrK: _attrK,
										attrV: _attrV
									}
								}
								getToAddSkuColumns().forEach(item => {
									const dataIndex = item.dataIndex
									if (dataIndex === 'cover') {
										newSku[dataIndex] = null
										newSku.coverFileId = 0
									} else {
										newSku[dataIndex] = 0
									}
								})
								return newSku
							}
							return sku
						})
					})
					.flat()
			},
			initialValue
		)
		let skus
		if (!canEditSkuAttrs.value) {
			skus = Tool.deepClone(editForm.value.skus)
		} else {
			skus = _skus
			skus.forEach(sku => {
				editForm.value.skus.forEach(_sku => {
					if (sku.vUuids && _sku.vUuids) {
						switch (sku.vUuids.length - _sku.vUuids.length) {
							case 0:
								if (_.isEqual(sku.vUuids, _sku.vUuids)) {
									getToAddSkuColumns().forEach(item => {
										sku[item.dataIndex] = _sku[item.dataIndex]
									})
								}
								break
							case 1:
								if (_.difference(_sku.vUuids, sku.vUuids).length === 0) {
									getToAddSkuColumns().forEach(item => {
										sku[item.dataIndex] = _sku[item.dataIndex]
									})
								}
								break
							case -1:
								if (_.difference(sku.vUuids, _sku.vUuids).length === 0) {
									getToAddSkuColumns().forEach(item => {
										sku[item.dataIndex] = _sku[item.dataIndex]
									})
								}
								break
							default:
								Api.notify(t('通知.sku属性值修改有误'))
								break
						}
					}
				})
			})
		}
		getToAddSkuColumns().forEach(item => {
			const _column = {
				title: item.title,
				dataIndex: item.dataIndex,
				align: 'center',
				width: 120,
				minWidth: 120
			}
			if (item.visible !== false) {
				skuColumns.value.push(_column)
			}
		})
		editForm.value.skus = Tool.deepClone(skus)
	}

	const selVSelSkus = () => {
		skuSelects.value = []
		editForm.value.skus.forEach(sku => {
			if (attrVSelects.value.length > 0) {
				sku.isSel = false
				const difference = _.difference(attrVSelects.value, sku.vUuids)
				if (difference.length === 0) {
					sku.isSel = true
					skuSelects.value.push(sku.key)
				}
			} else {
				sku.isSel = false
			}
		})
	}

	const changeSelSkus = (selectedRowKeys: any[], selectedRows: any[]) => {
		editForm.value.attrs.forEach(attr => {
			attr.v.forEach(_v => {
				_v.isSel = false
			})
		})
		skuSelects.value = selectedRowKeys
		editForm.value.skus.forEach(sku => {
			sku.isSel = !!_.includes(selectedRowKeys, sku.key)
		})
	}

	return {
		skuColumns,
		attrVSelects,
		skuSelects,
		getAttrVSelects,
		generateSkus,
		selVSelSkus,
		changeSelSkus
	}
}
